package com.wzz.Util;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import com.wzz.vo.ExcelHeaderTitle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class ExcelEportUtil {

    public void exportExcel(HttpServletResponse response, List<?> list){
        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter();
//自定义标题别名
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("age", "年龄");
        writer.addHeaderAlias("birthDay", "生日");
        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(2, "申请人员信息");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流
        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        String name = StringUtils.toUtf8String("申请学院");
        response.setHeader("Content-Disposition","attachment;filename="+name+".xls");
        ServletOutputStream out= null;
        try {
            out = response.getOutputStream();
            writer.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 关闭writer，释放内存
            writer.close();
        }
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    //
    public void exportExcel2(HttpServletResponse response, List<?> list){
        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter();
        //自定义标题别名
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("age", "年龄");
        writer.addHeaderAlias("birthDay", "生日");
        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(2, "申请人员信息");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        ServletOutputStream out= null;
        try {
            out = response.getOutputStream();
            writer.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 关闭writer，释放内存
            writer.close();
        }
    }

    public static void excelExport(String fileName, String sheetName, String title, List<ExcelHeaderTitle> titleAliasList, List<?> rows, HttpServletResponse response) throws IOException {
        // 生成xlsx
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.renameSheet(sheetName);

        titleAliasList.forEach(h -> writer.addHeaderAlias(h.getKey(), h.getName()));

        //合并单元格后的标题行，使用默认标题样式
       // writer.merge(titleAliasList.size() - 1, title);

        // 设置标题样式
        StyleSet style = writer.getStyleSet();
        CellStyle headCellStyle = style.getHeadCellStyle();
        //水平居中
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        //设置内容字体
        Font font = writer.createFont();
        //加粗
        font.setBold(true);
        //设置标题字体大小
        font.setFontHeightInPoints((short)12);

        headCellStyle.setFont(font);

        System.out.println(rows);

        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);

        //导出数据
        OutputStream out = null;
        try {
            //设置Http响应头告诉浏览器下载这个附件
//            response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
//            response.setHeader("Content-Transfer-Encoding", "binary");
            //设置响应头的类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
            //response.setHeader("Content-Disposition","attachment;filename=test.xls");
            response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");

//            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//            //让浏览器下载文件,name是上述默认文件下载名
//            response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");

            out = response.getOutputStream();
            writer.flush(out, true);
            // 关闭writer，释放内存
            writer.close();
            //此处记得关闭输出Servlet流
            IoUtil.close(out);
        } catch (Exception ex) {
            System.out.print("导出Excel异常，异常信息");
            throw new IOException("导出Excel异常，异常信息：" + ex.getMessage());
        } finally {
            //清理资源
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                System.out.print("清理资源错误");
            }
        }




    }
}
